package com.learng.springcloud.ctrl;

import com.google.common.collect.Lists;
import com.learng.springcloud.entity.Member;
import com.learng.springcloud.vo.MemberVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Created by zhouwei on 2020/7/22.
 */
@RestController
@RequestMapping("/member")
public class MemberCtrl {

    @Value("${server.port}")
    int serverPort;

    @RequestMapping("/getAll")
    public List<Member> getAll(){
//        await();
        return Lists.newArrayList(
                Member.builder().id(1L).name("至尊宝").age(20).build(),
                Member.builder().id(2L).name("紫霞仙子").age(18).build(),
                Member.builder().id(3L).name("端口").age(serverPort).build()
        );
    }

    @RequestMapping("/findByCondition")
    public List<Member> findByCondition(MemberVo condition){
        await();
        return mockMembers(condition);
    }

    @RequestMapping("/findByRequestParam")
    public List<Member> findByRequestParam(@RequestParam(value = "id", required = false) Long id,
                                           @RequestParam(value = "name", required = false) String name){
        await();
        MemberVo condition = new MemberVo();
        condition.setId(id);
        condition.setName(name);
        return mockMembers(condition);
    }

    @RequestMapping("/findByRequestBody")
    public List<Member> findByRequestBody(@RequestBody MemberVo condition){
        await();
        return mockMembers(condition);
    }

    private List<Member> mockMembers(MemberVo condition) {
        List<Member> memberList = Lists.newArrayList(
                Member.builder().id(1L).name("至尊宝").age(20).build(),
                Member.builder().id(2L).name("紫霞仙子").age(18).build()
        );

        List<Member> result = memberList.stream()
                .filter(member -> Objects.equals(member.getId(), condition.getId())
                        || Objects.equals(member.getName(), condition.getName())
                        || Objects.equals(member.getAge(), condition.getAge())
                        || (condition.getId() == null)
                        && StringUtils.isEmpty(condition.getName())
                        && condition.getAge() == null
                )
                .collect(Collectors.toList());

        result.add(Member.builder().id(3L).name("端口").age(serverPort).build());
        return result;
    }

    public void await(){
        int sleepMillis = new Random().nextInt(100);
        try {
            System.out.printf("await millis : %s", sleepMillis);
            System.out.println();
            Thread.sleep(sleepMillis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(sleepMillis > 50){
            throw new RuntimeException("11");
        }
    }

}
